package com.zhilianbao.leyaogo.ui.fragment.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bql.pulltorefreshandloadmore.loadmoreview.DefaultLoadMoreView;
import com.bql.pulltorefreshandloadmore.loadmoreview.LoadMoreRecyclerView;
import com.bql.pulltorefreshandloadmore.loadmoreview.OnLoadMoreListener;
import com.bql.pulltorefreshandloadmore.ultraptr.OnDefaultRefreshListener;
import com.bql.pulltorefreshandloadmore.ultraptr.PtrClassicFrameLayout;
import com.bql.pulltorefreshandloadmore.ultraptr.PtrFrameLayout;
import com.bql.pulltorefreshandloadmore.ultraptr.indicator.PtrIndicator;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.http.callback.LoadingViewCallback;
import com.zhilianbao.leyaogo.view.springview.SpringView;

import butterknife.BindView;
import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * ClassName: BaseMultiTypeFragment <br>
 * Description: 多类型的Fragment<br>
 * Author: Cyarie <br>
 * Created: 2016/12/14 20:26 <br>
 * Update Time：<br>
 * Update Description：<br>
 */
public abstract class BaseMultiTypeFragment extends BaseOkHttpFragment implements OnLoadMoreListener {


    @BindView(R.id.rcv_load_more)
    public LoadMoreRecyclerView mRcvLoadMore;

    //下拉刷新 上拉加载 风格
    @BindView(R.id.prt_layout)
    @Nullable
    public PtrClassicFrameLayout mPrtLayout;

    //推拽View
    @BindView(R.id.springView)
    @Nullable
    public SpringView mSpringView;

    /**
     * 数据类型集合
     */
    public Items mItems = new Items();

    /**
     * 多类型的Adapter
     */
    public MultiTypeAdapter mMultiTypeAdapter;

    //当前页数
    public int mCurPage = 1;

    //每页大小
    public int pageSize = 20;

    @Override
    protected void initLazyViewsAndEvents(@Nullable Bundle savedInstanceState) {
        super.initLazyViewsAndEvents(savedInstanceState);
        initLazyViews(savedInstanceState);
        initView();

    }

    /**
     * 懒加载 如果继承该类 要实现懒加载效果 重写该方法进行初始化
     *
     * @param savedInstanceState
     */
    protected void initLazyViews(@Nullable Bundle savedInstanceState) {

    }

    @Override
    protected void onEnterAnimationEnd(Bundle savedInstanceState) {
        super.onEnterAnimationEnd(savedInstanceState);
        if (!lazyLoadMode()) {
            initView();
        }

    }

    /**
     * 初始化View
     */
    private void initView() {
        if (getLayoutManager() == null) {
            throw new NullPointerException("The getLayoutManager() method must not be null");
        }
        mRcvLoadMore.setLayoutManager(getLayoutManager());
        if (getMultiAdapter() == null) {
            throw new NullPointerException("The getMultiAdapter() method must not be null");
        }

        mRcvLoadMore.setAdapter(getMultiAdapter());


        if (getItemDecoration() != null)
            mRcvLoadMore.addItemDecoration(getItemDecoration());
        mRcvLoadMore.setOnLoadMoreListener(this);
        mRcvLoadMore.setHideLoadingView(hideLoadMoreView());
        if (mPrtLayout != null) {
            mPrtLayout.disableWhenHorizontalMove(true);
            mPrtLayout.setOnRefreshListener(new OnDefaultRefreshListener() {
                @Override
                public void onRefreshBegin(PtrFrameLayout frame) {
                    onRefresh();
                }

                @Override
                public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                    return canRefresh() ? super.checkCanDoRefresh(frame, content, header) : false;
                }
            });
            mPrtLayout.setOnUIScrollChangeListener((frame, isUnderTouch, status, ptrIndicator) -> onUIScrollChange(frame, isUnderTouch, status, ptrIndicator));
        }
        mRcvLoadMore.setOnItemClickListener((holder, position) -> onRcvItemClick(holder, position));
        loadDataList(1, false);
    }

    @Override
    protected int getContentViewLayoutID() {
        return isSpringMode() ? R.layout.fragment_spring_load : R.layout.fragment_ptr_load;
    }

    /**
     * 加载数据列表
     *
     * @param curPage         当前页
     * @param isPullToRefresh 是否下拉刷新
     */
    public abstract void loadDataList(int curPage, boolean isPullToRefresh);


    public abstract void onRcvItemClick(RecyclerView.ViewHolder holder, int position);


    /**
     * 适配器 继承自{@link MultiTypeAdapter}
     *
     * @return
     */
    public abstract MultiTypeAdapter getMultiAdapter();


    /**
     * 添加分割线 {@link android.support.v7.widget.RecyclerView.ItemDecoration}
     *
     * @return
     */
    public abstract RecyclerView.ItemDecoration getItemDecoration();

    /**
     * 布局管理器 {@link android.support.v7.widget.RecyclerView.LayoutManager}
     *
     * @return
     */
    public abstract RecyclerView.LayoutManager getLayoutManager();


    @Override
    public void onDestroyView() {
        if (mRcvLoadMore != null && mRcvLoadMore.getLoadMoreView() instanceof DefaultLoadMoreView) {
            ((DefaultLoadMoreView) mRcvLoadMore.getLoadMoreView()).stop();
            mRcvLoadMore = null;
        }
        super.onDestroyView();
    }


    /**
     * 刷新数据
     */
    public abstract void onRefresh();

    /**
     * 滑动监听
     */
    public void onUIScrollChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {

    }

    /**
     * 是否可以下拉刷新 默认 true
     */
    public boolean canRefresh() {
        return true;
    }


    /**
     * 结束刷新和加载
     */
    public void completeRefreshAndLoad() {
        if (mPrtLayout != null) {
            mPrtLayout.onRefreshComplete();
        }
        if (mRcvLoadMore != null) {
            mRcvLoadMore.onLoadMoreComplete();
        }

    }

    /**
     * 刷新加载回调
     *
     * @param <T>
     */
    public abstract class RefreshAndLoadCallback<T> extends LoadingViewCallback<T> {

        public RefreshAndLoadCallback(boolean isPullToRefresh) {
            super(mActivity, getView(), isPullToRefresh);
        }


        @Override
        public void onAfter(T t, Exception e) {
            super.onAfter(t, e);
            if (isPullToRefresh)
                completeRefreshAndLoad();
        }
    }

    public boolean hideLoadMoreView() {
        return false;
    }

    public boolean isSpringMode() {
        return false;
    }

}
